package com.greendeliver.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.greendeliver.activities.base.BaseListActivity;
import com.greendeliver.business.ServiceManager;

public class MenuDetailActivity extends BaseListActivity {

	private ArrayList<HashMap<String, String>>	servicesInfos;
	private String								ID_MENU;
	private String								NAME_MENU;

	private ProgressDialog						pDialog;
	private Intent								i;

	private static final String					TAG_SUCCESS				= "success";
	private static final String					TAG_ID_MENU				= "ID_menu";
	private static final String					TAG_ID_DISH				= "ID_dish";
	private static final String					TAG_NAME_MENU			= "name_menu";
	private static final String					TAG_NAME_DISH			= "name_dish";
	private static final String					TAG_DESCRIPTION_DISH	= "description_dish";

	private TextView							textTitleMenu;

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setFullscreen();
		setContentView( R.layout.activity_menu_detail );

		this.servicesInfos = new ArrayList<HashMap<String, String>>();
		this.pDialog = new ProgressDialog( MenuDetailActivity.this );

		Intent i = getIntent();
		this.ID_MENU = i.getStringExtra( TAG_ID_MENU );
		this.NAME_MENU = i.getStringExtra( TAG_NAME_MENU );

		this.textTitleMenu = (TextView) findViewById( R.id.menu_name );
		textTitleMenu.setText( NAME_MENU );

		new Load().execute();

		ListView lv = getListView();
	}

	class Load extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute()
		{
			Log.d( "Coucou", "1" );
			super.onPreExecute();
			pDialog.setMessage( "Chargement des Plats" );
			pDialog.setIndeterminate( false );
			pDialog.setCancelable( false );
			pDialog.show();
			Log.d( "Coucou", "2" );
		}

		/**
		 * getting All products from url
		 * */
		@Override
		protected String doInBackground( String... args )
		{
			Log.d( "Coucou", "3" );
			ServiceManager sm = new ServiceManager();
			Log.d( "Coucou", "4" );
			servicesInfos = sm.get_dishes_by_ID( ID_MENU );
			Log.d( "Coucou", "5" );

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		@Override
		protected void onPostExecute( String file_url )
		{
			Log.d( "Coucou", "6" );
			pDialog.dismiss();
			Log.d( "Coucou", "7" );
			ListAdapter adapter = new SimpleAdapter( MenuDetailActivity.this,
					servicesInfos, R.layout.list_dish,
					new String[] { TAG_ID_DISH, TAG_NAME_DISH, TAG_DESCRIPTION_DISH },
					new int[] { R.id.id_dish, R.id.name_dish, R.id.description_dish } );
			setListAdapter( adapter );
			Log.d( "Coucou", "8" );
		}
	}

}
